package com.zhuozhou.jdyc.base;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.databinding.ViewDataBinding;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentPagerAdapter;
import androidx.viewpager.widget.PagerAdapter;
import androidx.viewpager.widget.ViewPager;

import com.zhuozhou.jdyc.R;
import com.zhuozhou.jdyc.wight.customlayout.TabLayout;

import java.util.Objects;

/**
 * Create by CherishTang on 2019/8/1
 * describe: tabLayout+viewpager侧滑fragment布局封装
 */
public abstract class BaseViewPagerFragment<VM extends BaseViewModel, VDB extends ViewDataBinding> extends BaseFragment<VM, VDB> implements ViewPager.OnPageChangeListener {
    public ViewPager mBaseViewPager;
    protected TabLayout tabLayout;
    public LinearLayout llTab;

    public BaseViewPagerAdapter adapter;

    @Override
    protected int getLayoutId() {
        return R.layout.base_tablayout_viewpager;
    }

    @Override
    protected void initView(View view, Bundle savedInstanceState) {
        mBaseViewPager = view.findViewById(R.id.viewpager);
        tabLayout = view.findViewById(R.id.tabLayout);
        llTab = view.findViewById(R.id.ll_tab);

        adapter = new BaseViewPagerAdapter(getChildFragmentManager(),
                FragmentPagerAdapter.BEHAVIOR_RESUME_ONLY_CURRENT_FRAGMENT, getPagers());
        mBaseViewPager.setAdapter(adapter);
        tabLayout.setupWithViewPager(mBaseViewPager);
        mBaseViewPager.addOnPageChangeListener(this);
        mBaseViewPager.setCurrentItem(0, true);
        tabLayout.setIndictorWidth(16);
        tabLayout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
            @Override
            public void onTabSelected(TabLayout.Tab var1) {
                TextView tv = (TextView) LayoutInflater.from(getActivity()).inflate(R.layout.tab_text_view, null);
                tv.setText(var1.getText());
                var1.setCustomView(tv);
            }

            @Override
            public void onTabUnselected(TabLayout.Tab var1) {
                var1.setCustomView(null);
            }

            @Override
            public void onTabReselected(TabLayout.Tab var1) {

            }
        });
    }

    public void setOverScrollMode(int scrollMode) {
        tabLayout.setOverScrollMode(scrollMode);
    }

    public int getCurrentItem() {
        return mBaseViewPager.getCurrentItem();
    }

    @Override
    protected void initData(Bundle bundle) {

    }

    protected abstract PagerInfo[] getPagers();

    public static class PagerInfo {
        private String title;
        private Class<?> clx;
        private Bundle args;

        public PagerInfo(String title, Class<?> clx, Bundle args) {
            this.title = title;
            this.clx = clx;
            this.args = args;
        }
    }

    public class BaseViewPagerAdapter extends FragmentPagerAdapter {
        private PagerInfo[] mInfoList;
        private Fragment mCurFragment;

        public BaseViewPagerAdapter(@NonNull FragmentManager fm, int behavior, PagerInfo[] mInfoList) {
            super(fm, behavior);
            this.mInfoList = mInfoList;
        }

        public void setPagerInfo(PagerInfo[] mInfoList) {
            this.mInfoList = mInfoList;
        }

        @Override
        public void setPrimaryItem(ViewGroup container, int position, Object object) {
            super.setPrimaryItem(container, position, object);
            if (object instanceof Fragment) {
                mCurFragment = (Fragment) object;
            }
        }

        public Fragment getCurFragment() {
            return mCurFragment;
        }

        public PagerInfo[] getPagerInfo() {
            return mInfoList;
        }

        @Override
        public Fragment getItem(int position) {
            PagerInfo info = mInfoList[position];
            return Fragment.instantiate(Objects.requireNonNull(getContext()), info.clx.getName(), info.args);
        }

        @Override
        public int getCount() {
            return mInfoList.length;
        }

        @Override
        public CharSequence getPageTitle(int position) {
            return mInfoList[position].title;
        }

        @Override
        public int getItemPosition(Object object) {
            return PagerAdapter.POSITION_NONE;
        }
    }

    @Override
    public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

    }

    @Override
    public void onPageSelected(int position) {

    }

    @Override
    public void onPageScrollStateChanged(int state) {

    }
}
